import React, {Component} from 'react';
import "./index.css"

class Item extends Component {

  state = {
    mouse: false
  }

  render() {
    const {id, done, name} = this.props;
    return (
      <li onMouseLeave={this.handleMouse(false)} onMouseEnter={this.handleMouse(true)}
          style={{backgroundColor: this.state.mouse ? '#ddd' : '#fff'}}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleCheckChange(id)}/>
          <span>{name}</span>
        </label>
        <button className="btn btn-danger" style={{display: this.state.mouse ? 'block' : 'none'}}
                onClick={this.deleteListOfItem(id)}>删除
        </button>
      </li>
    );
  }

  handleMouse = (flag) => {
    //  鼠标的移入和移出事件
    return () => {
      this.setState({mouse: flag})
    }
  }
  handleCheckChange = (id) => {
    return (e) => {
      this.props.handleChangeChecked(e.target.checked, id);
    }
  }
  deleteListOfItem = (id) => {
    //  点击删除的事件
    return () => {
      this.props.deleteItem(id);
    }
  }
}

export default Item;